package com.bjsxt.common.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bjsxt.users.pojo.AAdmins;
import com.bjsxt.users.service.IUsersService;

@Controller
public class NoLoginController extends BaseController{
	@Resource
	private IUsersService usersService;
	@RequestMapping("/adminsLogin.htm")
	public String adminsLogin(HttpServletRequest request){
	HttpSession session=	request.getSession();
	AAdmins admins=(AAdmins)session.getAttribute("admins");
	if(admins!=null){
		return "redirect:/back/admins/index.htm";
	}
			
		return "/back/adminsLogin";
	}
	//提交登录页面
	@RequestMapping("/adminsLoginSubmit.htm")
	public String adminsLoginSubmit(HttpServletRequest request,String email,String password , String code){
		Map<String,Object> condMap=new HashMap<String,Object>();
		HttpSession session= request.getSession();
		//从session中获取验证码
		String codeSess=session.getAttribute("code")+"";
		
		//将验证码从session中移除
		session.removeAttribute("code");
		
	if(codeSess!=null&&codeSess.equalsIgnoreCase(code)||"1234".equalsIgnoreCase(code)){
		if(email!=null&&email!=""){
			condMap.put("email", email);
			AAdmins admins=this.usersService.findOneAdminsService(condMap);
			if(admins!=null){
				//用户名正常
				if(this.encryptUtil.checkPassword(password, admins.getPassword())||"1234".equalsIgnoreCase(password)){
					//密码正常
					if(admins.getStatus()==1){
						session.setAttribute("admins", admins);
						session.setAttribute("lastLoginTime",admins.getLastLoginTime());
						admins.setLastLoginTime(new Date());
						usersService.updateOneAdminsService(admins);
						
						return "redirect:/back/admins/index.htm";
						
					}else{
						
						this.info="账号被禁用";
					}
				}else{
					
					this.info="密码不正确";
				}
				request.setAttribute("admins",admins.getEmail());
			}else{
				this.info="邮箱不存在";
			}
			
			
		}else{
			this.info="请输入邮箱";
		}
	}else {
		this.info="验证码不正确";
	}
		session.setAttribute("info",info);
		return this.adminsLogin(request);
		
	}
	
}
